<?php
/**
* @file $Id: AllowEdit.fnc.php 465 2007-04-24 20:12:32Z costerhout $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function AllowEdit($modname=false)
{	global $_FOCUS;

	if(!$modname)
		$modname = $_REQUEST['modname'];

	if($modname=='Students/Student.php' && $_REQUEST['category_id'])
		$modname = $modname.'&category_id='.$_REQUEST['category_id'];

	if(User('PROFILE')=='admin')
	{
		if(!$_FOCUS['AllowEdit'])
		{
			if(is_numeric(User('PROFILE_ID')))
				$_FOCUS['AllowEdit'] = DBGet(DBQuery("SELECT MODNAME FROM PROFILE_EXCEPTIONS WHERE PROFILE_ID='".User('PROFILE_ID')."' AND CAN_EDIT='N'"),array(),array('MODNAME'));
			else
				$_FOCUS['AllowEdit'] = DBGet(DBQuery("SELECT MODNAME FROM USER_EXCEPTIONS WHERE USERNAME='".User('USERNAME')."' AND CAN_EDIT='N'"),array(),array('MODNAME'));
		}

		if(!$_FOCUS['AllowEdit'])
			$_FOCUS['AllowEdit'] = array(true);

		if(count($_FOCUS['AllowEdit'][$modname]))
			return false;
		else
			return true;
	}
	else
		return $_FOCUS['allow_edit'];;
}

function AllowUse($modname=false)
{	global $_FOCUS;

	if(!$modname)
		$modname = $_REQUEST['modname'];

	$exists = false;
	foreach($_FOCUS['Menu'] as $modcat=>$programs)
	{
		if($programs[$modname])
			$exists = true;
	}

	if(!$exists && substr($modname,0,20)!='Students/Student.php')
		return false;

	if($modname=='Students/Student.php' && $_REQUEST['category_id'])
		$modname = $modname.'&category_id='.$_REQUEST['category_id'];

	if(!$_FOCUS['AllowUse'])
	{
		if(is_numeric(User('PROFILE_ID')))
			$_FOCUS['AllowUse'] = DBGet(DBQuery("SELECT MODNAME FROM PROFILE_EXCEPTIONS WHERE PROFILE_ID='".User('PROFILE_ID')."' AND CAN_USE='N'"),array(),array('MODNAME'));
		else
			$_FOCUS['AllowUse'] = DBGet(DBQuery("SELECT MODNAME FROM USER_EXCEPTIONS WHERE USERNAME='".User('USERNAME')."' AND CAN_USE='N'"),array(),array('MODNAME'));
	}

	if(!$_FOCUS['AllowUse'])
		$_FOCUS['AllowUse'] = array(true);

	if(count($_FOCUS['AllowUse'][$modname]))
		return false;
	else
		return true;
}

function ProgramLink($modname,$title='')
{
	if(AllowUse($modname))
		$link = '<A HREF=Modules.php?modname='.$modname.'>';
	if($title)
		$link .= $title;
	if(AllowUse($modname))
		$link .= '</A>';

	return $link;
}

?>
